package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import com.wing.cabinet.dao.CabAlarmSettingsDao;
import com.wing.cabinet.model.entity.CabAlarmSettings;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.request.CabAlarmSettingsForm;
import com.wing.cabinet.model.response.CabAlarmSettingsVO;
import com.wing.cabinet.model.response.CellInventorySpaceVO;
import com.wing.cabinet.model.response.CellSingleInventoryProductVO;
import com.wing.common.utils.JsonResult;
import com.wing.order.model.entity.InventoryAlarm;
import com.wing.order.model.entity.InventoryAlarmDetail;
import com.wing.order.service.InventoryAlarmDetailService;
import com.wing.order.service.InventoryAlarmService;
import com.wing.util.PulsarCommons;
import com.wing.util.PulsarUtils;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 库存告警-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CabAlarmSettingsServiceImpl extends ServiceImpl<CabAlarmSettingsDao, CabAlarmSettings> implements CabAlarmSettingsService {

    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    @Value("${pulsar.client.topicUrl}")
    private String topicUrl;

    @Override
    public int add(CabAlarmSettingsForm cabAlarmSettingsForm) {

        //校验

        CabAlarmSettings cabAlarmSettings = new CabAlarmSettings();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(CabAlarmSettings.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(cabAlarmSettingsForm, cabAlarmSettings, copyOptions);

        return baseMapper.insert(cabAlarmSettings);
    }

    @Override
    public List<CabAlarmSettingsVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CabAlarmSettingsVO> pageList4app(Page page, Map<String, Object> params) {
        List<CabAlarmSettingsVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CabAlarmSettingsVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public void sendMsg(CabAlarmSettings cabAlarmSettings,String enterpriseCode) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            syncDate(cabAlarmSettings,enterpriseCode);
                        } catch (Exception e) {
                            log.error("syn fail", e);
                        }
                    }

                });
            }
        });
    }


    // 数据同步
    private void syncDate(CabAlarmSettings cabAlarmSettings,String enterpriseCode) {
        // cab_alarm_settings同步
        if (cabAlarmSettings!=null){
            try {
                cabAlarmSettings.setSkuCode(enterpriseCode+"-"+cabAlarmSettings.getSkuCode());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", "addOrUpdate");
                jsonObject.put("data", cabAlarmSettings);
                PulsarUtils.send(topicUrl+ PulsarCommons.CAB_ALARM_SETTINGS,jsonObject.toJSONString());
            }catch (Exception e){
                log.error("sync CabAlarmSettingsServiceImpl err 同步失败",e);
//                cabAlarmSettings.setIsSyns(0);
//                this.updateById(cabAlarmSettings);
                if(null != cabAlarmSettings.getId()){
                    CabAlarmSettings cabAlarmSettingsUpdate = new CabAlarmSettings();
                    cabAlarmSettingsUpdate.setId(cabAlarmSettings.getId());
                    cabAlarmSettingsUpdate.setIsSyns(0);
                    this.updateById(cabAlarmSettingsUpdate);
                }
            }
        }

    }


}
